package com.yq.controller;


import com.yq.model.vo.BaseRespVo;
import com.yq.model.vo.BaseVO;
import com.yq.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @Autowired
    AccountService accountService;
    //访问的url可以这样理解
    @RequestMapping("/login")
    //返回的数据自动的解析为json
    @ResponseBody
    public BaseVO login(){
        BaseVO baseVO = new BaseVO();
        baseVO.setMsg("ok");
        baseVO.setStatus(200);
        return baseVO;
    }
    @RequestMapping("transfer")
    @ResponseBody
    public BaseRespVo transfer(HttpServletRequest request) {
        Integer fromId = Integer.valueOf(request.getParameter("fromId"));
        Integer destId = Integer.valueOf(request.getParameter("destId"));
        Integer money = Integer.valueOf(request.getParameter("money"));
        int code = accountService.remittance(fromId, destId, money);
        BaseRespVo baseRespVo = null;
        if (code==200){
            baseRespVo=BaseRespVo.ok(fromId+"给"+destId+"转了"+money);
        }else if (code==404){
            baseRespVo=BaseRespVo.error("转账失败");
        }else if (code==403){
            baseRespVo=BaseRespVo.error("余额不足");
        }
        return baseRespVo;
    }
}
